package com.xiaoyu.lanling.feature.moment.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.SimpleEventHandler
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.event.media.mediaselector.MediaSelectorResultEvent
import com.xiaoyu.lanling.event.moment.publish.ResultEvent
import com.xiaoyu.lanling.event.newtip.MomentCommentNoticeEvent
import com.xiaoyu.lanling.event.newtip.MomentLikeNoticeEvent
import com.xiaoyu.lanling.event.newtip.MomentNoticeEvent
import com.xiaoyu.lanling.feature.moment.MomentNoticeActivity
import com.xiaoyu.lanling.feature.moment.data.PublishData
import com.xiaoyu.lanling.feature.moment.datamodel.FeedBelongType
import com.xiaoyu.lanling.media.selector.MediaSelector
import com.xiaoyu.lanling.media.selector.MediaSelectorFromType
import com.xiaoyu.lanling.media.selector.MediaSelectorType.IMAGE
import com.xiaoyu.lanling.router.Router
import com.xiaoyu.lanling.util.TextViewUtil
import com.xiaoyu.lanling.view.FragmentAdapter
import com.xiaoyu.lanling.view.OnShowFragment
import com.xiaoyu.lanling.widget.tablayout.ScaleTabLayoutUtils
import kotlinx.android.synthetic.main.fragment_main_moment.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MainMomentFragment : OnShowFragment() {
    companion object {
        const val INDEX_SUBSCRIBE = 1
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return layoutInflater.inflate(R.layout.fragment_main_moment, container, false)
    }

    override fun onViewCreatedSafelyAfterAppFinishInit(view: View, savedInstanceState: Bundle?) {
        super.onViewCreatedSafelyAfterAppFinishInit(view, savedInstanceState)
        initView()
        initBind()
        initEvent()
    }

    private fun initView() {
        val fragmentList = mutableListOf<OnShowFragment>()
        fragmentList.add(RecommendMomentFragment())
        fragmentList.add(SubscribedMomentFragment())

        val titles = listOf(AppContext.getString(R.string.moment_recommend), AppContext.getString(R.string.moment_subscribe))
        ScaleTabLayoutUtils.createNormalScaleTabLayout(moment_tab_magic_indicator, moment_view_pager, titles, false)
        val adapter = FragmentAdapter(childFragmentManager, fragmentList, titles)
        moment_view_pager?.offscreenPageLimit = fragmentList.size
        moment_view_pager?.adapter = adapter
    }

    private fun initEvent() {
        AppEventBus.bindContainerAndHandler(this, object : SimpleEventHandler() {
            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: MediaSelectorResultEvent) {
                if (event.mediaPathList.isEmpty()) {
                    return
                }
                if (event.fromType != MediaSelectorFromType.MOMENT) {
                    return
                }
                when (event.type) {
                    IMAGE -> {
                        Router.instance.gotoPublishActivity(activity, event.type, event.mediaPathList, event.original)
                    }
                }
            }

            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: ResultEvent) {
                moment_view_pager?.currentItem = INDEX_SUBSCRIBE
            }

            @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
            fun onEvent(event: MomentNoticeEvent) {
                TextViewUtil.displayFor99PlusType(bottom_moment_new_notice_count, event.num)
            }

            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: MomentCommentNoticeEvent) {
                val pair = moment_notice_layout?.getViewTag<Pair<Int, Int>>() ?: Pair(0, 0)
                moment_notice_layout?.setViewTag(Pair(pair.first, event.num))
            }

            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: MomentLikeNoticeEvent) {
                val pair = moment_notice_layout?.getViewTag<Pair<Int, Int>>() ?: Pair(0, 0)
                moment_notice_layout?.setViewTag(Pair(event.num, pair.second))
            }
        })
    }

    private fun initBind() {
        publish_button?.setOnClickDebounceListener {
            val hasDraft = PublishData.hasDraft(FeedBelongType.MOMENT)
            if (hasDraft) {
                Router.instance.gotoPublishActivity(activity)
            } else {
                MediaSelector.instance.selectorForImage(PublishData.MAX_COUNT_PICTURE, true, MediaSelectorFromType.MOMENT, true)
            }
        }
        moment_notice_layout?.setOnClickDebounceListener {
            val pair = it.getViewTag<Pair<Int, Int>>() ?: Pair(0, 0)
            val pageIndex = if (pair.first > pair.second) MomentNoticeActivity.PAGE_INDEX_LIKE else MomentNoticeActivity.PAGE_INDEX_COMMENT
            activity?.let { Router.instance.gotoMomentRemindActivity(it, pageIndex) }
            //clear the tag
            moment_notice_layout?.setViewTag(null)
        }
    }
}